package two.chapter_1.c1_1;

import edu.princeton.cs.algs4.StdOut;

public class Ex1_1_18 {

    public static void main(String[] args) {

        StdOut.println(mystery(2,25));//50
        StdOut.println(mystery2(2,5));//

    }


    /**
     * 结果是  a*b   乘法的含义b个a相加
     * @param a
     * @param b
     * @return
     */
    private static int mystery(int a,int b){
       if (b==0){
           return 0;
       }
       if (b%2==0){
           return mystery(a+a,b/2);
       }
       return mystery(a+a,b/2)+a;

    }
    /**
     * 结果是  a^b
     * @param a
     * @param b
     * @return
     */
    public static int mystery2(int a, int b) {
        if (b == 0) {
            return 1;
        }
        if (b % 2 == 0) {
            return mystery2(a * a, b / 2);
        }
        return mystery2(a * a, b / 2) * a;
    }


}
